<template>
  <div class="department" id="department">
    <!-- 左侧部门管理 -->
    <div class="structureTree" v-if="treelength">
      <div class="structureTree_title">
        <div>组织机构</div>
        <div>
          <!-- <el-checkbox v-model="checked" @change="see()">查看下级</el-checkbox> -->
        </div>
      </div>
      <tree
        :treeData="treeData"
        :checkData="checkData"
        nodeKey="fCompanyid"
        @getTreeInfo="handleNodeClick"
        :defaultProps="defaultProps"
      >
      </tree>
    </div>
    <!-- 右侧 -->
    <div class="department_right">
      <!-- 机构名称 -->
      <el-breadcrumb :title="showName"></el-breadcrumb>
      <!-- 顶部导航栏 -->
      <div class="mechanism_nav">
        <!-- 左侧 -->
        <div class="mechanism_nav_left" style="width: 100%">
          <el-select
            class="selectBhz"
            v-model="getList.stationId"
            placeholder="所有拌和站"
            @change="selectBhz"
            style="width: 160px"
          >
            <el-option
              v-for="v in bhzDataList"
              :key="v.fItemName"
              :label="v.fItemName"
              :value="v.fItemValue"
            >
            </el-option>
          </el-select>
          <el-select
            class="selectBhz"
            v-model="getList.machineId"
            placeholder="所有拌和机"
            @change="yj_searchForm(getList)"
            style="width: 160px"
          >
            <el-option
              v-for="v in bhjDataList"
              :key="v.fItemName"
              :label="v.fItemName"
              :value="v.fItemValue"
            >
            </el-option>
          </el-select>

          <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="yj_searchForm(getList)"
            >查询
          </el-button>
        </div>
        <!-- 顶部按钮 -->
        <div class="print">
          <div class="printTable">
            <el-button
              type="primary"
              icon="el-icon-refresh"
              size="small"
              @change="yj_searchForm(getList)"
            >
            </el-button>

            <el-button
              type="primary"
              icon="el-icon-search"
              size="small"
              @click="editText(fId)"
              >查看
            </el-button>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="small"
              @click="Print()"
              >打印
            </el-button>
          </div>
        </div>
      </div>
      <!-- 表格 -->
      <div class="text_table">
        <tableData
          height="100%"
          :config="table_config"
          :dataList="tableData"
          :loading="loading"
          @click-change="rowChange"
          @ClickOnLine="yj_viewForm"
        />
      </div>

      <!--配合比通知单编号弹框 查看 -->
      <div class="Control-1 mechanism_dialogS closure">
        <el-dialog
          :title="dialogTitle"
          :modal-append-to-body="false"
          v-dialogDrag
          :visible.sync="dialogEditVisible"
          v-loading="loadingEditDialog"
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)"
        >
          <div
            class="dialogBody common"
            :style="{ height: '650px' }"
            style="overflow: hidden"
          >
            <div style="margin-bottom: 5px; height: 100%">
              <div class="commonBigBox">
                <div class="commonBox" style="border-right: 1px solid #ccc">
                  <tableData
                    :config="table_config_dialog"
                    :dataList="tableData_dialog"
                    :loading="loading"
                    @selection-change="selectionChange"
                    @sort-change="sortChange"
                    @click-change="rowChange_dialog"
                    @ClickOnLine="yj_viewForm_dialog"
                  />
                </div>
                <div class="commonBox">
                  <publicForm
                    ref="publicForm0"
                    lableWidth="95px"
                    :formObj="formObjText1"
                    :formData="row_dialog"
                    :timestamp="row_dialog.fScanfile"
                    :fileList="fSourcefileList"
                  >
                  </publicForm>
                </div>
              </div>

              <div style="height: 50px"></div>
            </div>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>
  
<script>
import tree from "@/components/public/el-trees"; //左侧机构树
import TableData from "@/components/HntManage/el-tables"; //主页面列表
import elBreadcrumb from "@/components/public/el-breadcrumb.vue"; //面包屑
import publicForm from "@/components/public/el-form.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import {
  getUserCompanyTreeList, //左侧机构树接口
  getMachineCheckLists, //主页面列表接口
  getStationmachineList, //拌合站列表
  getMachineListByStationId, //拌合机
  getMachineCheckListByFid, //配合比详情
} from "@/api/request.js";

export default {
  name: "EquipmentCalibration",
  data() {
    return {
      name: "EquipmentCalibration", //按钮配置页面代号
      yj_view: null,

      bhzDataList: [], //拌合站
      bhjDataList: [], //拌合机

      // 列表
      getList: {
        userId: "", // 查询待办或已办时传 列表页查询不传
        stationType: "1", //拌合机类型 1 - 砼拌合站 2-水稳拌合站 3-沥青拌合站
        companyId: "", // 当前登录用户标段id
        modelType: "2", // 0代办 1已办 2表示列表页查询
        stationId: "",
        machineId: "",
      },
      loading: false, //列表转载
      fId: null, //当前行数据fId
      table_config: formJs.table_config,
      tableData: [], //表格列表数据列表
      row: null, //当前行

      // 列表

      // 弹框
      table_config_dialog: formJs.table_config_dialog,
      tableData_dialog: [],
      row_dialog: formJs.row_dialog,
      fId_dialog: null, //当前行数据fId
      dialogTitle: "",
      loadingEditDialog: false, //弹框loading
      dialogEditVisible: false, //新增弹框是否显示
      formObjText1: formJs.formObjText_dialog,
      handleObj: formJs.handleObj, //场站
      // handleObjs1: formJs.handleObjs1,
      processObj: formJs.processObj, //现场
      // processObj1: formJs.processObjs1, //一级审核
      formData: formJs.formData,
      recordfiles: [], //删除文件
      fSourcefileList: [],
      stationfileList: [],
      saveData: formJs.saveData,
      // 弹框

      // 机构树
      highlightBd: true, //左侧机构树停留背景色
      treelength: false, //左侧树显示隐藏
      checked: true, //查看下级按钮
      showName: "未选择公司",
      companyTree: "", //所有数据
      treeData: [], //左侧树状图数据
      defaultProps: {
        children: "children",
        label: "fShortname",
      },
      treeText: {
        //树状图参数
        companyId: null,
        userId: null,
        text: null,
      },
      checkData: [], //树状图默认选中id
      // 机构树
      activeNames: ["0", "1"],
      userInfo: {}, //登录用户
    };
  },
  components: {
    tree, //左侧组织机构数
    TableData, //主页面列表
    elBreadcrumb, //面包屑
    publicForm, //弹框表格
  },
  mounted() {
    this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
    if (JSON.parse(sessionStorage.getItem("getList"))) {
      var getListStorage = JSON.parse(sessionStorage.getItem("getList"));

      this.getList = {
        stationType: "1", //拌合机类型 1 - 砼拌合站 2-水稳拌合站 3-沥青拌合站
        modelType: "2", // 0代办 1已办 2表示列表页查询
        companyId: getListStorage.fCompanyid, // 左侧选中组织机构id
        userId: this.userInfo.userId, // 查询待办或已办时传 列表页查询不传
        stationId: getListStorage.stationId,
        machineId: "",
      };
      this.getbhjDataMethods(this.getList.stationId);
    }
    if (JSON.parse(sessionStorage.getItem("treeCheck"))) {
      var id = JSON.parse(sessionStorage.getItem("treeCheck"));
      this.checkData = [id.fCompanyid];
    }
    this.getTree();
    this.btnRole(this.name);
  },
  methods: {
    btnRole(name) {
      var btnList = [];
      var ModuleList = JSON.parse(sessionStorage.getItem("ModuleList"));
      if (ModuleList.length > 0) {
        ModuleList.map((item) => {
          if (item.fEncode == name) {
            btnList = item.modulebuttonList;
          }
        });
        var btnStr = [];
        btnList.map((item) => {
          btnStr.push(item.fEncode);
        });

        this.yj_view = btnStr.includes("yj_view");
      } else {
        this.yj_view = true;
      }
    },
    // 左侧树结构
    getTree() {
      this.treeText = {
        companyId: this.userInfo.companyId,
        userId: this.userInfo.userId,
        text: "1", // 0其他  1 砼拌合站 2水稳 3沥青
      };
      getUserCompanyTreeList(this.treeText)
        .then((res) => {
          if (res.errcode == 0) {
            var companyTree = res.data.userCompanyTreeList;
            this.treeData = this.commonJs.tranListToTreeData(companyTree);

            if (this.treeData[0].children == undefined) {
              this.treelength = false; //左侧组织机构是否显示
            } else {
              this.treelength = true;
            }

            if (this.checkData[0]) {
              this.showName = JSON.parse(
                sessionStorage.getItem("treeCheck")
              ).fShortname;
              this.getList.companyId = this.checkData[0];
            } else {
              this.showName = this.treeData[0].fShortname; //当前点击组织机构名称
              this.getList.companyId = this.treeData[0].fCompanyid; //当前点击组织机构ID
              this.checkData[0] = this.treeData[0].fCompanyid;
            }
            this.mixingPlantList("0", this.getList);
            this.yj_searchForm(this.getList); // 调用主页面列表接口
            // 列表
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch(() => {
          return false;
        });
    },

    // 点击左侧机构树
    handleNodeClick(data) {
      this.showName = data.fShortname; //顶部显示机构名
      this.getList = {
        stationType: "1", //拌合机类型 1 - 砼拌合站 2-水稳拌合站 3-沥青拌合站
        modelType: "0", // 0代办 1已办 2表示列表页查询
        companyId: data.fCompanyid, // 左侧选中组织机构id
        userId: this.userInfo.userId, // 查询待办或已办时传 列表页查询不传
        stationId: "",
        machineId: "",
      };

      this.mixingPlantList("0", this.getList);
      this.yj_searchForm(this.getList);
    },
    // 获取列表数据
    yj_searchForm(id) {
      this.loading = true;
      var getListStorage = {};
      if (JSON.parse(sessionStorage.getItem("getList"))) {
        getListStorage = JSON.parse(sessionStorage.getItem("getList"));
        getListStorage.stationId = this.getList.stationId;
        getListStorage.machineId = this.getList.machineId;
      } else {
        getListStorage = {
          endDates: "",
          fCloseoperation: "",
          fCompanyid: "",
          keyWord: "",
          machineId: this.getList.machineId,
          page: 1,
          pageSize: 20,
          startDates: "",
          beginDates: "", //上传--开始
          overDates: "", // 上传--结束
          stationId: this.getList.stationId,
          strengthgrade: "",
          warningLevel: "",
        };
      }
      window.sessionStorage.setItem("getList", JSON.stringify(getListStorage));

      getMachineCheckLists(id)
        .then((res) => {
          if (res.errcode == 0) {
            var that = this;
            that.loading = false;
            that.tableData = res.data.machineCheckLists;
          } else {
            this.$message.error("列表", res.msg);
            this.loading = false;
          }
        })
        .catch(() => {
          console.log("列表");
          this.loading = false;
        });
    },

    // 获取拌合站数据
    mixingPlantList(num, datas) {
      var data = {
        companyId: datas.companyId, // 施工标段id
        modelType: num, // 类型
        stationType: "1", // 拌合站类型
      };
      getStationmachineList(data)
        .then((res) => {
          if (res.errcode == 0) {
            var arr = res.data.stationmachineList;
            this.bhzDataList = [
              {
                fItemName: "所有拌和站",
                fItemValue: "",
              },
            ];
            arr.map((item) => {
              this.bhzDataList.push({
                fItemName: item.fMachinename,
                fItemValue: item.fMachineid,
              });
            });
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    selectBhz(e) {
      this.getList.machineId = "";
      this.getbhjDataMethods(e);
      this.yj_searchForm(this.getList);
    },
    getbhjDataMethods(data) {
      getMachineListByStationId({ stationId: data })
        .then((res) => {
          if (res.errcode == 0) {
            var arr = res.data.stationmachineList;

            this.bhjDataList = [
              {
                fItemName: "所有拌和机",
                fItemValue: "",
              },
            ];
            arr.map((item) => {
              this.bhjDataList.push({
                fItemName: item.fMachinename,
                fItemValue: item.fMachineid,
              });
            });
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    // 点击当前
    rowChange(val) {
      this.row = val;
      this.fId = val.fId;
      console.log("大列表", val);
    },
    rowChange_dialog(val) {
      this.row_dialog = val;
      this.fId_dialog = val.fId;
    },
    // 点击查看
    yj_viewForm(row, label) {
      this.fId = row.fId;
      if (label == "检定/校准次数") {
        this.editText(row.fId);
      }
    },
    yj_viewForm_dialog(row, label) {
      console.log(label);
      this.row_dialog = row;
      this.fId_dialog = row.fId;
      this.fSourcefileList = row.scanFileList;
    },
    // 查看
    editText(id) {
      this.dialogTitle = "查看";
      if (id != "" && id != null) {
        this.loadingEditDialog = true; //弹框loading
        this.dialogEditVisible = true;
        getMachineCheckListByFid({ fId: id })
          .then((res) => {
            if (res.errcode == 0) {
              console.log(333);
              this.loadingEditDialog = false;
              this.tableData_dialog = res.data.machineCheckList;

              if (this.tableData_dialog.length != 0) {
                this.row_dialog = this.tableData_dialog[0];
                this.fSourcefileList = this.row_dialog.scanFileList;
              }
            } else {
              this.$message.error("查看", res.msg);
              this.loadingEditDialog = false;
            }
          })
          .catch(() => {
            console.log("查看---详情接口报错");
            this.loadingEditDialog = false;
          });
      } else {
        return this.$message.error("请先选择需要查看的数据");
      }
    },

    // 折叠面板
    changeCollapse(e) {
      console.log(this.activeNames, e);
    },

    // 获取页面容器高度
    getHeight() {
      // var h = document.documentElement.clientHeight; //获取页面可见高度
    },

    selectionChange() {},
    // 排序方法
    sortChange() {},
  },
};
</script>
  
<style scoped >
.printTables {
  background: #fff;
  display: flex;
  justify-content: end;
  padding: 5px;
  margin-top: 10px;
}

.dialogBody {
  padding: 3px 20px;
  height: 80%;
  overflow-y: auto;
}

.baseTitle {
  /* border: 1px solid #0c204d; */
  border-bottom: 1px solid #0c204d;
  height: 30px;
  line-height: 30px;
  position: relative;
  padding-left: 15px;
}

.baseTitle::after {
  display: block;
  content: "";
  height: 30px;
  width: 5px;
  position: absolute;
  left: 0;
  top: 0;
  background: #0c204d;
}
/* 弹框样式 */
.commonBigBox {
  width: 100%;
  height: 100%;
  display: flex;
}
.commonBox {
  width: 50%;
  height: 100%;
}
/* 弹框样式 */
</style>
